﻿#include "dialog.h"
#include<QVBoxLayout>
#include<QMessageBox>
#include<mainwindow.h>
int uniqueCarId;//这样做的目的是允许多个源文件共享同一个全局变量，而不需要在每个文件中重新定义它。
int uniqueFactoryId;
//看看是否实现了将数据插入到了数据库中
Dialog::Dialog(QSqlRelationalTableModel *car,QSqlTableModel *factory,QDomDocument details,QFile *output,QWidget *parent)
{
    carModel=car;
    factoryModel=factory;
    carDetails=details;
    outputFile=output;
    QGroupBox *inputWidgetBox=createInputwidgets();
    QDialogButtonBox *buttonBox=createButtons();
    //界面布局
    QVBoxLayout *layout=new QVBoxLayout;
    layout->addWidget(inputWidgetBox);
    layout->addWidget(buttonBox);
    setLayout(layout);
    setWindowTitle(tr("添加产品"));
}

//槽函数
void Dialog::submit()//提交
{
    QString factory=factoryEditor->text();
    QString address=addressEditor->text();
    QString name=carEditor->text();
    if(factory.isEmpty()||address.isEmpty()||name.isEmpty())
    {
        QString message(tr("请输入厂名、厂址和商品名称!"));
        QMessageBox::information(this,tr("添加产品"),message);
    }else {
        int factoryId=findFactoryId(factory);
        if(factoryId==-1)
        {
            factoryId=addNewFactory(factory,address);
        }
        int carId=addNewCar(name,factoryId);
        QStringList attribs;
        attribs=attribEditor->text().split(";",Qt::SkipEmptyParts);
        addAtrribs(carId,attribs);
        accept();//后面试试不用这个函数的效果,用于自动关闭对话框
}
}

void Dialog::revert()//撤销,只是简单的清空列表，并不是从数据库中撤销
{
    factoryEditor->clear();
    addressEditor->clear();
    carEditor->clear();
    yearEditor->setValue(QDate::currentDate().year());
    attribEditor->clear();
}

//普通函数

int Dialog::findFactoryId(const QString &factory)
{
    int row=0;
    while (row<factoryModel->rowCount()) {
        QSqlRecord record=factoryModel->record(row);
        if(record.value("manufactory")==factory)
        {
            return record.value("id").toInt();
        }else {
            row++;
        }
    }
    return -1;
}

int Dialog::addNewCar(const QString &name,int factoryId)
{
    QSqlRecord record;
    int id=generateCarId();
    QSqlField f1("carid",QVariant::Int);
    QSqlField f2("name",QVariant::String);
    QSqlField f3("factoryid",QVariant::Int);
    QSqlField f4("year",QVariant::Int);
    f1.setValue(QVariant(id));
    f2.setValue(QVariant(name));
    f3.setValue(QVariant(factoryId));
    f4.setValue(QVariant(yearEditor->value()));
    record.append(f1);
    record.append(f2);
    record.append(f3);
    record.append(f4);
    carModel->insertRecord(-1,record);
    return id;
}
int Dialog::addNewFactory(const QString &factory,const QString &address)
{
    QSqlRecord record;
    int id=generateFactoryId();
    QSqlField f1("id",QVariant::Int);
    QSqlField f2("manufactory",QVariant::String);
    QSqlField f3("address",QVariant::String);
    f1.setValue(QVariant(id));
    f2.setValue(QVariant(factory));
    f3.setValue(QVariant(address));
    record.append(f1);
    record.append(f2);
    record.append(f3);
    factoryModel->insertRecord(-1,record);
    return id;
}
/*将录入的车型信息写入XML文件*/
void Dialog::addAtrribs(int carId,QStringList attribs)
{
    /*创建一个car标签*/
    QDomElement carNode=carDetails.createElement("car");
    carNode.setAttribute("id",carId);
    for(int i=0;i<attribs.count();i++)
    {
        QString attribNumber=QString::number(i+1);
        if(i<10)
            attribNumber.prepend("0");
        QDomText textNode=carDetails.createTextNode(attribs.at(i));
        QDomElement attribNode=carDetails.createElement("attrib");
        attribNode.setAttribute("number",attribNumber);
        attribNode.appendChild(textNode);
        carNode.appendChild(attribNode);
    }
    QDomNodeList archive=carDetails.elementsByTagName("archive");
    archive.item(0).appendChild(carNode);
    if(!outputFile->open(QIODevice::WriteOnly))
    {
        return;
    }else {
        QTextStream stream(outputFile);
        archive.item(0).save(stream,4);
        outputFile->close();
    }
}
QDialogButtonBox *Dialog::createButtons()//?
{
    QPushButton *closeButton=new QPushButton(tr("关闭"));
    QPushButton *revertButton=new QPushButton(tr("撤销"));
    QPushButton *submitButton=new QPushButton(tr("提交"));
    closeButton->setDefault(true);
    connect(closeButton,SIGNAL(clicked(bool)),this,SLOT(close()));
    connect(revertButton,SIGNAL(clicked(bool)),this,SLOT(revert()));
    connect(submitButton,SIGNAL(clicked(bool)),this,SLOT(submit()));

    QDialogButtonBox *buttonBox=new QDialogButtonBox;//测试一下以下代码
    buttonBox->addButton(submitButton,QDialogButtonBox::RejectRole);
    buttonBox->addButton(revertButton,QDialogButtonBox::RejectRole);
    buttonBox->addButton(closeButton,QDialogButtonBox::RejectRole);

    return buttonBox;
}
QGroupBox *Dialog::createInputwidgets()
{
    QGroupBox *box=new QGroupBox(tr("添加商品"));
    QLabel *factorylabel=new QLabel(tr("制造商:"));
    QLabel *addresslabel=new QLabel(tr("厂址:"));
    QLabel *carlabel=new QLabel(tr("品牌:"));
    QLabel *yearlabel=new QLabel(tr("上市时间:"));
    QLabel *attriblabel=new QLabel(tr("产品属性(由分号;隔开):"));
    factoryEditor=new QLineEdit;
    carEditor=new QLineEdit;
    addressEditor=new QLineEdit;
    yearEditor=new QSpinBox;
    yearEditor->setMinimum(1900);
    yearEditor->setMaximum(QDate::currentDate().year());
    yearEditor->setValue(yearEditor->maximum());
    attribEditor=new QLineEdit;
    QGridLayout *layout=new QGridLayout;
    layout->addWidget(factorylabel,0,0);
    layout->addWidget(factoryEditor,0,1);
    layout->addWidget(addresslabel,1,0);
    layout->addWidget(addressEditor,1,1);
    layout->addWidget(carlabel,2,0);
    layout->addWidget(carEditor,2,1);
    layout->addWidget(yearlabel,3,0);
    layout->addWidget(yearEditor,3,1);
    layout->addWidget(attriblabel,4,0,1,2);
    layout->addWidget(attribEditor,5,0,1,2);
    box->setLayout(layout);
    return box;
}

int Dialog::generateCarId()
{
    uniqueCarId+=1;
    return uniqueCarId;
}
int Dialog::generateFactoryId()
{
    uniqueFactoryId+=1;
    return uniqueFactoryId;
}
